<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("moduleFunction.php");

$errMsg = "";
if(isset($_REQUEST["reqtype"])){
	if($_REQUEST["reqtype"] == "save"){
		$errMsg .= moduleMod();
	}else if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "delete"){
		$errMsg .= moduleDel();
	}
	$module = sxREQUEST("module");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	$doc_name = sxREQUEST("doc_name");
}else{
	$module = "";
	$description = "";
	$note = "";
	$doc_name = "";
}

if(isset($_REQUEST["mod"])){
	$module = $_REQUEST["mod"];
	$sSQL = "SELECT * FROM module " .
				"WHERE module = '".$module."'";
	$objSDAO->query($sSQL, "mod");
	if($row = $objSDAO->fetch("mod")){
		$module = $row["module"];
		$description = $row["description"];
		$note = $row["note"];			
	}else{
		$errMsg .= _t("module '%s' not found",$module)."<br>";
	}
	//seleziono i doc associati al modulo
	$sSQL = "SELECT * FROM module_doc WHERE module='$module'";
	$objSDAO->query($sSQL, "doc");
}

//output HTML
$aOpz = array("tit2"=>_t("Aggiornamento modulo"), "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			var objDA =  document.getElementById("module");
			var objDe =  document.getElementById("description");
			var objMessArea =  document.getElementById("messArea");
			var errMsg = "";
			if(objDe.value == ""){
				errMsg += "<?php echo _t("description missing.")?><br>";
				objDe.focus();
			}
			if(objDA.value == ""){
				errMsg += "<?php echo _t("name  missing.")?><br>";
				objDA.focus();
			}
			if(errMsg == ""){
				objFrm.submit();
			}else{
				objMessArea.innerHTML = errMsg;
			}
		}catch(e){
			raiseError(e.description, "save new data area");
		}			
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"moduleMod.php\" method=\"POST\" enctype=\"multipart/form-data\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");
echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Save data"),
										"text"=>_t("save")
										),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>"cancel"
										)
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Module")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Name")."(*)</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"hidden\" name=\"module\" id=\"module\" maxlength=\"4\" value=\"$module\">$module</td>" .
		"</tr>\n");echo("</form>");
		
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description")."(*)</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"description\" id=\"description\" maxlength=\"80\" size=\"80\" value=\"".sxHE($description)."\"></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Document")."</td>" .
			"<td class=\"text01\"><input class=\"inpt01\" type=\"text\" name=\"doc_name\" id=\"doc_name\" maxlength=\"80\" size=\"32\" value=\"$doc_name\">&nbsp;&nbsp;<input class=\"inpt01\" type=\"file\" name=\"document\" id=\"document\"></td>" .
		"</tr>\n");
echo("</table>");
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td width=\"90%\" colspan=\"2\">&nbsp;</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"link01\" colspan=\"2\">"._t("Documents")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"tabh01\" width=\"90%\">"._t("file")."</td>" .
			"<td class=\"tabh01\" width=\"90%\">"._t("delete")."</td>" .
		"</tr>\n");
$numDoc = 0;
while($row = $objSDAO->fetch("doc")){
	$numDoc++;
	echo("<tr>" .
			"<td class=\"text01\"><a class=\"text01\" target=\"blank\" href=\"getDoc.php?mod=$module&fn=".$row["file_name"]."&xsn=".XSN."\">".sxHE($row["name"])."</a></td>" .		
			"<td class=\"text01\">" .
			"<input class=\"inpt01\" type=\"checkbox\" name=\"elim_$numDoc\" id=\"elim_$numDoc\">" .
			"<input type=\"hidden\" name=\"fn_elim_$numDoc\" id=\"fn_elim_$numDoc\" value=\"".sxHE($row["file_name"])."\">" .
			"</td>" .
		"</tr>\n");
}
if($numDoc == 0){
	echo("<tr>" .
			"<td class=\"text01\" colspan=\"2\">"._t("No documents")."</td>" .			
		"</tr>\n");
}
echo("</table>");
echo(commandsHTML($aCmd));

echo("</td>\n</tr>\n</table>\n");
echo("</form>");
echo(footerHTML());
